$(document).ready(function() {
	$( ".invest-container" ).addClass("ui-widget");
	$( ".invest-data-div input:last" ).addClass("text ui-widget-content ui-corner-all prompt");
	$( ".invest-data-div label span" ).css("color","#f2422b");
	$( ".invest-button" ).button();
	var country_select = $( "select[name='country']" ),
	farmer_select = $( "select[name='farmer']" ),
	crop_select = $( "select[name='crop']" );
	amount_input = $( "input[name='amount']" );
	amount_input.focus(function(){
		var value = $(this).val();
		if($(this).hasClass("prompt")){
			$(this).val("");
			$(this).removeClass("prompt");
			$(this).blur(function(){
				if($(this).val()===""){
					$(this).val(value);
					$(this).addClass("prompt");
				}
			});
		}
	});
	$.getJSON('assets/data/country.json', function(data){
	    $.each(data.countries, function(index, country){
			country_select.append(
				"<option value='"+country.id+"'>"+country.name+"</option>"
			);
	    });
	}).fail(function(){
		alert("Fail: maybe the data does not exists or is not json...");
	});
	country_select.combobox({
		select: function(event,ui){	
			var value = country_select.val();
			resetCountry();
			if(value!==""){
				farmer_select.html("");
				farmer_text.val("Select farmer");
				$.getJSON('assets/data/country_farmer.json', function(data){
					$.each(data.farmers, function(index, farmer){
						if(farmer.id_country===value){
							farmer_select.append(
								"<option value='"+farmer.id+"'>"+farmer.name+"</option>"
							);
						}
					});
				}).done(function(){
					farmer_select.combobox();	
				}).fail(function(){
					alert("Fail: maybe the farmers data does not exists or is not json...");
				});
			}
		}
	});
	farmer_select.combobox({
		select: function(event,ui){	
			var value = farmer_select.val();
			resetFarmer();
			if(value!==""){
				crop_select.html("");
				crop_text.val("Select crop");
				$.getJSON('assets/data/farmer_crop.json', function(data){
					$.each(data.crops, function(index, crop){
						if(crop.id_farmer===value){
							crop_select.append(
								"<option value='"+crop.id+"'>"+crop.description+"</option>"
							);
						}
					});
				}).done(function(){
					crop_select.combobox();	
				}).fail(function(){
					alert("Fail: maybe the crops data does not exists or is not json...");
				});
			}
		}
	});
	crop_select.combobox();
	var country_text = $( ".invest-data-div .custom-combobox:first .custom-combobox-input" ),
	farmer_text = $( ".invest-data-div .custom-combobox:eq(1) .custom-combobox-input" ),
	crop_text = $( ".invest-data-div .custom-combobox:last .custom-combobox-input" );
	country_text.val("Select country").attr("name","country_input");
	farmer_text.val("Select farmer").attr("name","farmer_input");
	crop_text.val("Select crop").attr("name","crop_input");
	var error = $( ".invest-container .error-message" ).css("color","#f2422b");
	var amountRegex = /^(\d+(\.\d*)?)|(\.\d+)$/;
	function updateErrorMessage( m ) {
		error
		.hide()
		.text( m )
		.show("pulsate",500);
		setTimeout(function() {
			error.removeClass( "ui-state-highlight", 1500 );
		}, 500 );
	}
	function checkRegexp( o, regexp, m ) {
		if (!(regexp.test(o.val()))){
			o.addClass( "ui-state-error" );
			updateErrorMessage( m );
			return false;
		} else {
			o.removeClass("ui-state-error");
			error.hide();
			return true;
		}
	}
	function validateFields(){
		amount_input.removeClass("ui-state-error");
		error.hide();
		var valid = true;
		if(amount_input.hasClass("prompt") || country_text.val()==="Select country" ||
		   farmer_text.val()==="Select farmer" || crop_text.val()==="Select crop"){
			updateErrorMessage("All form fields are required");
			valid = false;
		}else{
			valid = valid && checkRegexp( amount_input,amountRegex, "Enter a valid amount" );
		}
		return valid;
	}
	$( ".invest-data-div .custom-combobox:eq(0)" ).click(function(e) {
		e.stopPropagation();
		var text = country_text.val();
		if(crop_text.val()===""){
			crop_text.val("Select crop");
		}
		if(farmer_text.val()===""){
			farmer_text.val("Select farmer");
		}
		if(text==="Select country"){
			country_text.val("");
		}
	});
	$( ".invest-data-div .custom-combobox:eq(1)" ).click(function(e) {
		e.stopPropagation();
		var text = farmer_text.val();
		if(country_text.val()===""){
			country_text.val("Select country");
		}
		if(crop_text.val()===""){
			crop_text.val("Select crop");
		}
		if(text==="Select farmer"){
			farmer_text.val("");
		}
	});
	$( ".invest-data-div .custom-combobox:eq(2)" ).click(function(e) {
		e.stopPropagation();
		var text = crop_text.val();
		if(farmer_text.val()===""){
			farmer_text.val("Select farmer");
		}
		if(country_text.val()===""){
			country_text.val("Select country");
		}
		if(text==="Select crop"){
			crop_text.val("");
		}
	});
	$('html').click(function() {
		if(crop_text.val()===""){
			crop_text.val("Select crop");
		}
		if(country_text.val()===""){
			country_text.val("Select country");
		}
		if(farmer_text.val()===""){
			farmer_text.val("Select farmer");
		}
	});
	function resetCountry(){
		country_text.val("Select country");
		farmer_text.val("Select farmer");
		crop_text.val("Select crop");
	}
	function resetFarmer(){
		crop_text.val("Select crop");
		farmer_text.val("Select farmer");
	}
	$( ".invest-button" ).click(function(){
		if(validateFields()){
			alert("All is correct! (This code is in the 182 line of assets/js/invest.js file, do something here)");
		};
	});
});